package com.mycompany.app;


// https://developers.redhat.com/blog/2020/05/20/getting-started-with-the-fabric8-kubernetes-java-client#


import java.io.File;
import java.nio.file.Files;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.fabric8.kubernetes.client.dsl.PodResource;
import io.fabric8.kubernetes.client.KubernetesClientException;

public class App
{
    public App() {}

    public static void main(String[] args) {
        System.out.println("Begin ...");

        try {
 
            // use user kubeconfig
            //
            File configFile = new File("/root/.kube/config");
            final String configYAML = String.join("\n", Files.readAllLines(configFile.toPath()));
            Config config = Config.fromKubeconfig(configYAML);
            config.setTrustCerts(true);

            // 
            // use config first
            // use service account
            
            // Config config = new ConfigBuilder().withMasterUrl("https://kubernetes.default.svc").build();
            KubernetesClient client = new DefaultKubernetesClient(config);
            
            client.pods().inNamespace("default").list().getItems().forEach(
                    pod -> System.out.println(pod.getMetadata().getName()));

            System.out.println("Finish ...");

        } catch(Exception e){
            System.out.println(" ========== Exception =================== ");
            e.printStackTrace();
        }

        return;
    }
}
